package com.powernode.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.IndexImg;
import com.powernode.service.IndexImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/indexImg")
@Api(tags = "轮播图")
public class AdminIndexImgController {


    @Autowired
    private IndexImgService indexImgService;


    @GetMapping("/page")
    @ApiOperation(value = "轮播图管理分页")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public ResponseEntity<Page<IndexImg>> indexImgPage(Page<IndexImg> page, IndexImg indexImg) {
        page = indexImgService.indexImgPage(page, indexImg);
        return ResponseEntity.ok(page);
    }

    @PostMapping
    @ApiOperation(value = "轮播图新增")
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    public ResponseEntity<Void> addIndexImg(@RequestBody IndexImg indexImg) {
            indexImgService.save(indexImg);
        return ResponseEntity.ok().build();
    }


    @GetMapping("/info/{id}")
    @ApiOperation(value = "轮播图图片id管理")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public ResponseEntity<IndexImg> indexImgInfo(@PathVariable("id") Long id) {
       IndexImg indexImg= indexImgService.getIndexImgInfoById(id);
        return ResponseEntity.ok(indexImg);
    }


    @GetMapping("/indexImgs")
    @ApiOperation(value = "小程序轮播图图片")
    public ResponseEntity<List<IndexImg>> indexImgs() {
        List<IndexImg> list = indexImgService.list();
        return ResponseEntity.ok(list);
    }
}
